<?php
/**
 * Created by PhpStorm.
 * User: jungshen
 * Date: 2018/8/2
 * Time: 14:45
 */

namespace app\api\controller\other;

use app\api\controller\BasicApi;
use think\Db;
use think\facade\Request;

class Article extends BasicApi
{

    /**
     * @Notes: 所有文章列表
     * @return \think\response\Json
     * @author: Forska
     * @email: 736523388@qq.com
     * @DateTime: 2018/10/24 16:34
     */
    public function alllist(){
        $page = Request::param('page',1);
        $pagesize = Request::param('pagesize',10);
        $title = Request::param('title');
        $db = Db::table('article')->where('status',1);
        if(!empty($title)){
            $db->where('title','like','%'.$title.'%');
        }
        $list = $db->page($page,$pagesize)->order('sort,create_at desc')->select();
        foreach ($list as &$item) {
            $item['images'] = explode('|',$item['images']);
            $item['create_at'] = date('Y-m-d',$item['create_at']);
        }
        return json(['msg'=>'success','data'=>$list],200);
    }
    public function detail(){
        $id = Request::param('id');
        $map = [
            'id'=>array('eq',$id)
        ];
        $info = Db::table('article')->where($map)->find();
        if(empty($info) || $info['status'] != 1){
            return json(['msg'=>'找不到'],404);
        }
        $info['create_at'] = date('Y-m-d',$info['create_at']);
        $info['images'] = explode('|',$info['images']);
        $info['praised'] = DB::name('praise')->where('attach_id',$id)->where('type',3)->count();
        Db::table('article')->where($map)->setInc('borwse');
        return json(['msg'=>'success','data'=>$info],200);
    }
}